<?php

namespace Wx\Controller;
use Think\Controller;

class MyGrouporderController extends CommonController {

    // 个人中心-拼团订单列表
    public function index(){
        $status = intval(I('type'));    //当type为0时，获取不到
        if($status){
            if($status==9){//当type为0时，获取不到，自行转换
                $map['o.state'] = 0;
            }else{
                $map['o.state'] = $status;
            }// end if
        }else{
            $status = '';
        }// end if
        $map['o.u_id'] = session('u_id');
        $list = M('shop_spellgroup_order')->table(C('DB_PREFIX')."shop_spellgroup_order as o")
                ->join(C('DB_PREFIX')."shop_spellgroup_copy as c ON c.id = o.c_id")
                ->field('o.*,c.sg_id,c.num')
                ->where($map)
                ->order('id desc')
                ->select();
        foreach($list as $k => $v){// 查询拼团商品信息，拼团的商家或者农户的信息
            // 根据sg_id查询拼团商品信息
            $list[$k]['goods_info'] = M('shop_spellgroup')->where('id='.$v['sg_id'])->field('u_id,s_id,type,title,h_imgs,actual_price,price,min_num')->find();
            // 处理主图
            $list[$k]['goods_info']['h_imgs'] = isMorePic($list[$k]['goods_info']['h_imgs']);
            // 查询商家或农户的姓名与头像
            $list[$k]['store_info'] = getMember($list[$k]['goods_info']['u_id'],'nickname,avatar');
            // 查询店铺名称
            if($list[$k]['goods_info']['s_id']!=0){
                $list[$k]['store_info']['storename'] = getStore($list[$k]['goods_info']['s_id']);
            }else{
                $list[$k]['store_info']['storename'] =  M('member_farmers')->where('u_id = '.$list[$k]['goods_info']['u_id'])->getField('village');
            }// end if

        }// end foreach
        $this -> assign('status',$status);
        $this -> assign('list',$list);
        $this->display('order');
    }// end fun index()


    // 订单详情
    public function orderdetail(){
        $id = intval(I('id'));
        $map['id'] = $id;
        $map['u_id'] = session('u_id');
        $info = getInfo('shop_spellgroup_order',$map);
        if($info){
            // 查询拼团副本中拼团商品id,以及已拼商品的份数
            $c_info =  getInfo('shop_spellgroup_copy',array('id'=>$info['c_id']),'c_id,sg_id,num');
            // 根据sg_id查询拼团商品信息
            $goods_info = getInfo('shop_spellgroup',array('id'=>$c_info['sg_id']),'u_id,limit_state,s_id,state,type,title,endtime,h_imgs,actual_price,price,min_num');
            $goods_info['h_imgs'] = isMorePic($goods_info['h_imgs']);   // 处理主图
            $goods_info['endtime'] = $goods_info['endtime']-time();    //处理拼团失效时间
            // 查询店铺老板或者农户的头像、昵称
            $store_info = getMember($goods_info['u_id'],'nickname,avatar');
            if($goods_info['type']==1){// 社区拼团-查询店铺名称
               $goods_info['storename'] = getStore($goods_info['s_id']);
            }else{// 乡村拼团查询农户乡村名称
                $goods_info['storename'] = M('member_farmers')->where('u_id = '.$goods_info['u_id'])->getField('village');
            }// end if
            // 获取代收店铺-即查询店铺中小区id与副本中小区id相同的店铺
            $goods_info['storename1'] = M('shop_store')->where(array('c_id' => $c_info['c_id'] ))->getField('storename');

            // 待成团情况下，获取所有订单的用户信息
            if($info['state']==1){
                // 该拼团所有的订单信息
                $allorder= M('shop_spellgroup_order')->where(array('state'=>1,'c_id'=>$info['c_id']))->select();
                // 订单信息中所有的用户信息
                foreach($allorder as $k => $v){
                    $group_member[$k] = getMember($v['u_id'],'nickname,avatar,hometown');
                }// end foreach
                $this -> assign('group_member',$group_member);
            }// end if

            if($goods_info['type']==1){// 社区拼团-那么地址永远为社区的地址
                $address = M('shop_store')->where('id='.$goods_info['s_id'])->getField('address');
            }else{// 乡村拼团-获取乡村名称
                $community = M('shop_community')->where(array('id' => $c_info['c_id'] ))->find();
                // 省市区信息+小区信息
                $pro = M('area')->where('id='.$community['a_id_1'])->getField('name');
                $city = M('area')->where('id='.$community['a_id_2'])->getField('name');
                $area = M('area')->where('id='.$community['a_id_3'])->getField('name');
                // 提货地址
                $address = $pro.$city.$area.$community['title'];
            }// end if

            $this -> assign('info',$info);              // 订单信息
            $this -> assign('c_info',$c_info);          // 拼团情况
            $this -> assign('goods_info',$goods_info);  // 拼团商品信息
            $this -> assign('store_info',$store_info);  // 店铺老板信息
            $this -> assign('address',$address);    // 地址
            $this->display();
        }else{

            $this->error('没有记录');

        }// end if



    }// end fun orderdetail()


    // 拼团-确认订单
    public function confirmorder(){
        // 前往支付
        if(IS_POST){
            // 接受post数据：商品id、购买份数、地址信息中包含有小区id、
            $post = I('post.');
            $copy = M('shop_spellgroup_copy');
            if(!$post['c_id']){//没有开团，则直接开团
                $c_info = $copy->where(array('sg_id'=>$post['sg_id'],'c_id'=>$post['comm_id']))->getField('id');
                if($c_info){
                    $post['c_id'] = $c_info;
                }else{
                    $data['sg_id'] = $post['sg_id'];       //商品id
                    $data['c_id'] = $post['comm_id'];   //小区id
                    $data['addtime'] = time();             //开团时间
                    $post['c_id'] = $copy->add($data);     //新增数据id
                }
            }
            $post['u_id'] = session('u_id');
            $post['order_sn'] = 'OS'.date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);       //生成订单号
            $post['qrcode'] = D("CreateReceive")->receive($post['order_sn']);
            $post['receive'] = '';
            for ($i = 0;$i<6;$i++){
                $post['receive'] .= mt_rand(0, 9);
            }
            $post['addtime'] = time();

            $price = M('shop_spellgroup')->where('id='.$post['sg_id'])->getField('actual_price');
            $post['total_fee'] = $post['num']*$price;
            $result = M('shop_spellgroup_order')->add($post);     //新增数据id
            $url = 'http://www.xiangtws.com/Api/Weixinpay/choosePay?order_id='.$result;
//             前往支付
            header('location:'.$url);
        }else{
            $g_id = intval(I('id'));        //获取商品id
            $c_id = intval(I('copy_id'));    //获取副本id
            // 用户信息
            $member_info = getMember(session('u_id'),'realname,u_type,mobile,c_id');
            // 根据g_id获取商品信息,以及用户所能购买的最大份数
            $goods_info = getInfo('shop_spellgroup',array('id'=>$g_id),'id,limit_state,u_id,s_id,type,title,server,endtime,h_imgs,actual_price,price,min_num,num');
            $goods_info['h_imgs'] = isMorePic($goods_info['h_imgs']);   // 处理主图
            $num  = M('shop_spellgroup_copy')->where(array('orsuccess'=>array('neq',2),'sg_id'=>$goods_info['id']))->sum("num");
            $goods_info['max_num'] = $goods_info['num'] - $num;
            // 查询商品拼团区间
            $price_range = M('spellgroup_price_range')->where('sg_id='.$g_id)->select();
            // 查询店铺老板或者农户的昵称、头像
            $store_info = getMember($goods_info['u_id'],'nickname,avatar');
            if($goods_info['type']==1){// 社区拼团-获取店铺名称
                $goods_info['storename'] = getStore($goods_info['s_id']);   //店铺名称
            }else{// 乡村拼团-获取乡村名称
                $goods_info['storename'] = M('member_farmers')->where('u_id = '.$goods_info['u_id'])->getField('village');  //农户乡村名称
            }// end if
            if($c_id){//如果存在拼团副本，则订单地址为副本中的小区地址
                $allorder= M('shop_spellgroup_order')->where(array('state'=>array('EGT',1),'c_id'=>$c_id))->select();
                // 订单信息中所有的用户信息
                foreach($allorder as $k => $v){
                    $group_member[$k] = getMember($v['u_id'],'nickname,avatar,hometown');
                }// end foreach
                $copy_info =  getInfo('shop_spellgroup_copy',array('id'=>$c_id),'id,c_id,num,orsuccess');
                // 查询收货地址
                if($goods_info['type']==1){// 社区拼团-那么地址永远为社区的地址
                    $shop_store = M('shop_store')->where('id='.$goods_info['s_id'])->field('c_id,address')->find();
                    // 提货地址
                    $comm_info['id'] = $shop_store['c_id'];
                    $comm_info['address'] = $shop_store['address'];
                }else{// 乡村拼团-获取乡村名称
                    $community = M('shop_community')->where(array('id' => $copy_info['c_id'] ))->find();
                    // 省市区信息+小区信息
                    $pro = M('area')->where('id='.$community['a_id_1'])->getField('name');
                    $city = M('area')->where('id='.$community['a_id_2'])->getField('name');
                    $area = M('area')->where('id='.$community['a_id_3'])->getField('name');
                    // 提货地址
                    $comm_info['id'] = $copy_info['c_id'];
                    $comm_info['address'] = $pro.$city.$area.$community['title'];
                }// end if
                $goods_info['storename1'] = M('shop_store')->where('c_id='.$copy_info['c_id'])->getField('storename');
                $this -> assign('group_member',$group_member);
            }else{// 未开团情况
                if($goods_info['type']==1){// 门店拼团-地址为社区门店的地址
                    $shop_store = M('shop_store')->where('id='.$goods_info['s_id'])->field('c_id,address')->find();
                    $comm_info['id'] = $shop_store['c_id'];
                    $comm_info['address'] = $shop_store['address'];
                    $goods_info['storename1'] = $goods_info['storename'];
                }else{// 乡村拼团-地址为用户小区地址
                    $community = M('shop_community')->where(array('id' => $member_info['c_id'] ))->find();
                    // 省市区信息+小区信息
                    $pro = M('area')->where('id='.$community['a_id_1'])->getField('name');
                    $city = M('area')->where('id='.$community['a_id_2'])->getField('name');
                    $area = M('area')->where('id='.$community['a_id_3'])->getField('name');
                    $comm_info['id'] = $member_info['c_id'];
                    $comm_info['address'] = $pro.$city.$area.$community['title'];
                    // 获取乡村拼团自提地址
                    $goods_info['storename1'] = M('shop_store')->where(array('c_id' => $member_info['c_id'] ))->getField('storename');
                }// end if
                $copy_info = 1;
            }// end if

            $this -> assign('copy_info',$copy_info);
            $this -> assign('member_info',$member_info);
            $this -> assign('u_type',$member_info['u_type']);
            $this -> assign('store_info',$store_info);
            $this -> assign('comm_info',$comm_info);
            $this -> assign('goods_info',$goods_info);//dump($goods_info);
            $this -> assign('price_range',$price_range);
            $this->display();
        }// end if

    }//end fun confirmorder


    // 未付款情况下-取消订单
    public function cancle(){
        $id = intval(I('order_id'));
        if($id){
            $info = getInfo('shop_spellgroup_order',array('id'=>$id,'u_id'=>session('u_id')));
            if ($info['state']==0){
                $info['state'] = 8;     //更改状态
                $res =  M('shop_spellgroup_order')->save($info);
                if($res){
                    $this->success('取消成功');
                }else{
                    $this->error('取消失败，请联系管理员');
                }
            }
        }else{
            $this->error('缺少参数');
        }// end if
    }// end fun cancle()





    // 添加评价
    public function addEva(){
        if(IS_POST){
            $post = I('post.');
            $post['u_id'] = session('u_id');    // 用户id
            if($post['img_1']!=''||$post['img_1']!=''||$post['img_1']!=''){//如果有图片便进行处理
                $post['pic'] = $post['img_1'].$post['img_2'].$post['img_3'];    //拼接图片路径
                $post['pic'] = substr($post['pic'],0,strlen($post['pic'])-1);   //去掉路径最后的英文逗号
            }// end if
            $post['addtime'] = time();
            $res = M('evaluate')->add($post);
            if($res) {
                // 更新订单状态-已评价
                M('shop_spellgroup_order')->where('id = '.$post['order_id'])->setField('state',7);
                $this->success('添加成功',U('Wx/User/user'));
            }else{
                $this->error('添加成功，请检查');
            }// end if
        }else{
            $sg_id = intval(I('sg_id'));  // 商品id
            $order_id = intval(I('order_id'));  //订单id
            $this -> assign('order_id',$order_id);
            $this -> assign('sg_id',$sg_id);
            $this->display('evaluate');
        }// end if
    }// end fun addEva()



}